/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.sensor.greenseeker;


import nl.cloudfarming.client.sensor.MalformedFileFormatException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;
import nl.cloudfarming.client.model.SensorMetadata;
import static org.junit.Assert.*;
import org.junit.Test;

/**
 *
 * @author Gerben Feenstra
 */
public class GreenseekerFileProcessorIT  {

    static final String DATEFILEFORMAT = "CYYYYMMDDCCCCCC.dbf";

    static final File NORMAL_SENSORFILE = new File("./src/test/resources/M20100905_NORMAL_TEST.dbf");
    static final File BORNE_SENSORFILE = new File("./src/test/resources/M20100531_BORNE_TEST.dbf");


    //MakeDateFromFileName()
    @Test
    public void makeDateFromFileNameTest() throws ParseException, java.text.ParseException,  MalformedFileFormatException, IOException{
        //Setup

        GreenseekerPreference.SENSOR_DATA_FILE_FORMAT.saveValue(DATEFILEFORMAT);
        String filename = "M20100905AB.dbf";
        GreenseekerFileProcessorNormal processor =  new GreenseekerFileProcessorNormal(File.createTempFile(filename,".dbf"));


        GregorianCalendar calendar = new GregorianCalendar();
        calendar.set(2010, 8, 5, 00 ,00 ,00 ); // Month starts at 0, 8 = september
        calendar.getTime().toString();

        Date returnDate = processor.getDateFromFileName(new File(filename));

        //test
        //In gregorian calendar the miliseconds can't be set. so must divide everything by 1000
        assertEquals(returnDate.getTime()/1000,calendar.getTimeInMillis()/1000);
    }

    @Test
    public void buildMetdataFromPreferencesTest() throws IOException{
        //Setup

        GreenseekerPreference.SENSOR_DATA_FILE_FORMAT.saveValue(DATEFILEFORMAT);
        GreenseekerFileProcessorNormal processor =  new GreenseekerFileProcessorNormal(File.createTempFile("greenseeker",".dbf"));
        //Test
        assertEquals("CYYYYMMDDCCCCCC.dbf", ((SensorMetadata)processor.buildMetadataFromPreferences()).getDateFormat());
    }

    //MakeDateFromFileName
    @Test (expected = AssertionError.class)
    public void EmptyFileFormatPropertyTest() throws ParseException, java.text.ParseException, MalformedFileFormatException, IOException{
        //Setup

        GreenseekerPreference.SENSOR_DATA_FILE_FORMAT.saveValue("");
        String filename = "M20100905AB.dbf";
        GreenseekerFileProcessorNormal processor =  new GreenseekerFileProcessorNormal(File.createTempFile(filename,".csv"));
        //test
        Date returnDate = processor.getDateFromFileName(new File(filename));
    }

    //MakeDateFromFileName
    @Test (expected = MalformedFileFormatException.class)
    public void MalformedFileFormatExceptionTest() throws ParseException, java.text.ParseException,  MalformedFileFormatException,  IOException{
        //Setup
        GreenseekerPreference.SENSOR_DATA_FILE_FORMAT.saveValue("DD");
        String filename = "M20100905AB.dbf";
        GreenseekerFileProcessorNormal processor =  new GreenseekerFileProcessorNormal(File.createTempFile(filename,".csv"));
        //test
        Date returnDate = processor.getDateFromFileName(new File(filename));
    }

    //MakeDateFromFileName
    @Test (expected = ParseException.class)
    public void ParseExceptionTest() throws ParseException, java.text.ParseException,  MalformedFileFormatException, IOException{
        //Setup

        GreenseekerPreference.SENSOR_DATA_FILE_FORMAT.saveValue(DATEFILEFORMAT); // CYYYYMMDDCCCCCC.dbf
        String filename = "M2010SEP05AB.dbf";
        GreenseekerFileProcessorNormal processor =  new GreenseekerFileProcessorNormal(File.createTempFile(filename,".dbf"));
        //test
        Date returnDate = processor.getDateFromFileName(new File(filename));
    }

    @Test
    public void createFileProcessorWithFile(){
        assertTrue(NORMAL_SENSORFILE.exists());
        GreenseekerFileProcessorNormal gfp = new GreenseekerFileProcessorNormal(NORMAL_SENSORFILE);
        // No exception should be thrown.
    }
        
    @Test(expected=java.lang.AssertionError.class)
    public void createFileProcessorWithEmptyFile(){
        GreenseekerFileProcessorNormal gfp = new GreenseekerFileProcessorNormal(null);
    }

    // Parsing is covered by this test as well
    @Test
    public void processNormalSuccesTest() throws IOException{
        assertTrue(NORMAL_SENSORFILE.exists());
        GreenseekerFileProcessorNormal gfpNormal = new GreenseekerFileProcessorNormal(NORMAL_SENSORFILE);
        gfpNormal.process();
    }

    // Parsing is covered by this test as well
    @Test
    public void processBorneSuccesTest() throws IOException{
        assertTrue(BORNE_SENSORFILE.exists());
        GreenseekerFileProcessorBorne gfpBorne = new GreenseekerFileProcessorBorne(BORNE_SENSORFILE);
        gfpBorne.process();
    }
}
